import Api from './Api';
import {AppHttpRequestUtils} from './AppHttpRequestUtils';
import {Constant} from './Constant';
import {DragElements} from './bean/DragElements.js';
import ConfigInfo from './bean/ConfigInfo';
class DataModel {
  constructor(config, contactElements = new DragElements(), originalData = {}, configInfo = new ConfigInfo()) {
    this.api = new Api();
    this.config = config;
    this.contactElements = contactElements;
    this.customizationIndex = 0;
    this.originalData = originalData;
    this.configInfo = new ConfigInfo();
  }
  saveConfigInfo(configInfo, appId, recordId) {
    const configInfoString = JSON.stringify(configInfo);
    const record = {};
    record[`${this.config[`${Constant.CONFIG_CONFIG_INFO_ID}`]}`] = {'value': configInfoString};
    return this.api.updateAppRecord(appId, recordId, record);
  }
  getAppElementInfos(record) {
    const appRequests = [];
    if (`${this.config[`${Constant.CONFIG_FIRST_APP_ID}`]}` !== ''
    && record[`${this.config[`${Constant.CONFIG_FIRST_APP_ID}`]}`].value !== undefined &&
     record[`${this.config[`${Constant.CONFIG_FIRST_APP_ID}`]}`].value !== '') {
      appRequests.push(AppHttpRequestUtils.getAppElementInfo(record[`${this.config[`${Constant.CONFIG_FIRST_APP_ID}`]}`].value));
    }
    if (`${this.config[`${Constant.CONFIG_SECOND_APP_ID}`]}` !== ''
    && record[`${this.config[`${Constant.CONFIG_SECOND_APP_ID}`]}`].value !== undefined &&
     record[`${this.config[`${Constant.CONFIG_SECOND_APP_ID}`]}`].value !== '') {
      appRequests.push(AppHttpRequestUtils.getAppElementInfo(record[`${this.config[`${Constant.CONFIG_SECOND_APP_ID}`]}`].value));
    }
    return appRequests;
  }
}
export default DataModel;